package com.essence.common.model.parser;

import com.essence.common.model.Packet;
import io.netty.buffer.ByteBuf;

/**
 * 带时标单点遥信
 */
public class MSpTbParser extends ByteBufParser {
    @Override
    void parse(Packet packet, ByteBuf buf) {
        if (packet.getVsq().getSq() == 0) {
            for (int i = 0; i < packet.getVsq().getNum(); i++) {
                int infoAdr = buf.readUnsignedMediumLE();
                byte qd = buf.readByte();
                byte[] cp56Time2aBytes = new byte[7];
                buf.readBytes(cp56Time2aBytes);
                Packet.CP56Time2a cp56Time2a = new Packet.CP56Time2a(cp56Time2aBytes);
                Packet.DigitalSignals digitalSignals = new Packet.DigitalSignals(infoAdr, qd, 1, cp56Time2a);
                packet.addDigitalSignals(digitalSignals);
            }
        }
        if (packet.getVsq().getSq() == 1) {
            int infoAdr = buf.readUnsignedMediumLE();
            for (int i = 0; i < packet.getVsq().getNum(); i++) {
                byte qb = buf.readByte();
                byte[] cp56Time2aBytes = new byte[7];
                buf.readBytes(cp56Time2aBytes);
                Packet.CP56Time2a cp56Time2a = new Packet.CP56Time2a(cp56Time2aBytes);
                Packet.DigitalSignals digitalSignals = new Packet.DigitalSignals(infoAdr + 1, qb, 1, cp56Time2a);
                packet.addDigitalSignals(digitalSignals);
            }
        }
    }

    @Override
    int infoObjectLength() {
        return 11;
    }
}
